/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.demo.consumer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.Future;

import com.alibaba.dubbo.common.HeaderProvider2;
import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.rpc.AsynInvoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.ResultListener;
import com.alibaba.dubbo.rpc.RpcContext;

import cn.aruan.dcp.service.Echo;

public class DemoAction {
    
    private Echo  echoService;
    private AsynInvoker invoker;

    public void setEchoService(Echo demoService) {
        this.echoService = demoService;
        this.invoker = (AsynInvoker)demoService;
        HeaderProvider2 provider = (HeaderProvider2)this.echoService;
        provider.getHeaders().put("session-id", "iamsorry");
        //provider.getHeaders().put("codec", "neg,gzip");
        provider.getHeaders().put("serializer", "json");
       // System.out.println(provider.getHeaders().get("cookie"));
    }

	public void start() throws Exception {
		ResultListener listener =  new ResultListener(){
			@Override
			public void onResult(Result result) {
				RpcContext context = RpcContext.getContext();
            	for(Map.Entry<String, String> entry : context.getRequestHeaders().getHeaders().entrySet()){
            		System.out.println(entry.getKey() +"->" + entry.getValue());
            	}
				System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + result.getValue());
				System.out.println("thread-" + Thread.currentThread().getId());
			}
			
		};
		//this.invoker.setResultListener(listener);
		StringBuffer sb = new StringBuffer();
		for(int i = 0;i< 500;i++){
			sb.append(i);
		}
		String s = sb.toString();
		long t = System.currentTimeMillis();
        for (int i = 0; i < 10000; i ++) {
        	
        	 
            try {
            	String hello = echoService.echo(s);
            	RpcContext context = RpcContext.getContext();
            	for(Map.Entry<String, String> entry : context.getResponseHeaders().getHeaders().entrySet()){
            		System.out.println(entry.getKey() +"->" + entry.getValue());
            	}
                System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello);
            } catch (Exception e) {
                e.printStackTrace();
            }
            long now = System.currentTimeMillis();
            if((now - t) >= 1000*60*3){
            	System.out.println("3 minutes=>" + i);
            	break;
            }
            
        	/*
        	Future<String> future = this.invoker.asynCall("sayHello", new Class[]{String.class}, new Object[]{listener,
        			"world" + i
        	});
        	
        	System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "-"+ i + "] " );
        	System.out.println("main thread-" + Thread.currentThread().getId());
        	System.out.println(future.isDone());
            Thread.sleep(200);
            System.out.println(future.isDone());
            */
            //Thread.sleep(2000);
        }
	}

}